var gwList;
var qqcs;
var tmp_gwdm;

$(function () {
    $(window).resize(function(){
        setTimeout(function () {
            fitCoulms();
        },300);
    });
    //表格初始化
    initDataGrid();
    //查询数据
    queryGwLb();
});

function initDataGrid() {
    // 主表格
    var fields = [[{
        title: '岗位分类代码',
        field: 'GWFLDM',
        sortable: false,
        hidden: true
    }, {
        title: '岗位分类名称',
        field: 'GWFLMC',
        sortable: false,
        width: 200,
        formatter: function (value) {
            if (null == value) {
                value = "";
            }
            return "<span title='" + value + "'>" + value + "</span>";
        }
    }, {
        title: '岗位代码',
        field: 'GWDM',
        sortable: false,
        hidden: true
    }, {
        title: '岗位名称',
        field: 'GWMC',
        sortable: false,
        width: 200,
        formatter: function (value) {
            if (null == value) {
                value = "";
            }
            return "<span title='" + value + "'>" + value + "</span>";
        }
    }, {
        title: '角色描述',
        field: 'JSMS',
        sortable: false,
        width: 300,
        formatter: function (value) {
            if (null == value) {
                value = "";
            }
            return "<span title='" + value + "'>" + value + "</span>";
        }
    }, {
        title: '操作',
        field: 'CZ',
        sortable: false,
        width: 100,
        formatter: zcFormatter
    }]];

    $('#tt').datagrid({
        collapsible: false,
        rownumbers: true,
        remoteSort: false,
        nowrap: true,
        striped: true,
        fitColumns: true,
        singleSelect: true,
        fit: false,
        pagination: true,
        columns: fields
    });

    //角色window初始化
    $('#js_win').window({
        width: 350,
        height: 300,
        modal: true,
        closed: true,
        title: '角色树',
        closable: true,
        draggable: false,
        resizable: false,
        iconCls: 'icon-save'
    });
}

//查询岗位列表
function queryGwLb() {
    //视角类别
    var sjlb = $('#sjDm').val();
    //岗位分类
    var gwfl = $('#gwflDm').val();
    //检索内容
    var jsnr = $('#jsnr').val();
    qqcs = {"sjlb": sjlb, "gwfl": gwfl, "jsnr": jsnr};
    //查询开始
    $('#tt').datagrid('clearSelections');
    $('#tt').datagrid({url: "/portal/admin/positionrole/queryGwLb.do", queryParams: qqcs});
}

//操作列内容
function zcFormatter(val, row, index) {
    return '<a href="#" onclick="XgJs(' + index + ')">修改</a>';
}

//修改角色
function XgJs(xh) {
    //获取岗位代码
    tmp_gwdm = $('#tt').datagrid('getRows')[xh]['GWDM'];
    qqcs = {"gwdm": tmp_gwdm};
    openProgress();
    $.ajax({
        url: "/portal/admin/positionrole/queryJsByGwDm.do",
        data: qqcs,
        type: "post",
        timeout: sys_timeout,
        dataType: "json",
        async: false,
        error: function (XMLHttpRequest, textStatus) {
            closeProgress();
            innerToast.warn("抱歉，系统暂时无法访问,请稍后再试");
            console.log(textStatus);
        },
        success: function (respText) {
            closeProgress();
            if (checkResponse(respText)) {
                //更新树
                $('#js_tree').tree({
                    onlyLeafCheck: false,//仅叶子节点可以被选中
                    checkbox: true,//复选框
                    data: respText.repData.jsList,
                    onCheck: function (node, checked) {
                        var childList = $(this).tree('getChildren', node.target);
                        if (childList.length > 0) {
                            var checkedTrue = function () {
                                childList.map(function (currentValue) {
                                    $("div[node-id='" + currentValue.id + "']").find(".tree-checkbox").removeClass("tree-checkbox0").addClass("tree-checkbox1");
                                })
                            };
                            var checkedFalse = function () {
                                $.each(childList, function (index, currentValue) {
                                    $("div[node-id='" + currentValue.id + "']").find(".tree-checkbox").removeClass("tree-checkbox1").addClass("tree-checkbox0");
                                })
                            };
                            var checkChangeProperties = checked == true ? checkedTrue() : checkedFalse();
                        }
                    }
                });
                $('#js_win').window('open');
            }
            else {
                innerToast.warn(respText.msg);
            }
        }
    });
}

//保存岗位角色
function saveGwJs() {
    //获取选中的节点
    var nodes = $('#js_tree').tree('getChecked');
    var str_nodes = "";
    for (var i = 0; i < nodes.length; i++) {
        if (null != nodes[i].children) {
            continue;
        } else if ("" == str_nodes) {
            str_nodes = nodes[i].id;
        } else {
            str_nodes = str_nodes + "," +
                nodes[i].id;
        }
    }
    qqcs = {"gwdm": tmp_gwdm, "nodes": str_nodes};
    openProgress();
    $.ajax({
        url: "/portal/admin/positionrole/saveGwJs.do",
        data: qqcs,
        type: "post",
        timeout: sys_timeout,
        dataType: "json",
        async: false,
        error: function (XMLHttpRequest, textStatus) {
            closeProgress();
            innerToast.warn("抱歉，系统暂时无法访问,请稍后再试");
            console.log(textStatus);
        },
        success: function (respText) {
            closeProgress();
            if (checkResponse(respText)) {
                innerToast.info("保存成功!");
                $('#js_win').window('close');
                $('#tt').datagrid('clearSelections');
                $('#tt').datagrid('reload');
            }
            else {
                innerToast.warn(respText.msg);
            }
        }
    });
}

function openProgress() {
    $.messager.progress({
        title: '提示',
        msg: '请稍候……',
        text: ''
    });
}

function closeProgress() {
    $.messager.progress('close');
}